package edu.chinasoft.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.Goods;
import edu.chinasoft.pojo.PageUtilForGoods;
import edu.chinasoft.service.GoodsService;
import edu.chinasoft.service.GoodsServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月16日:下午5:17:31
 * @version V1.0
 * @Description: 分页查找商品
 */
@WebServlet(name = "myFindGoodsPageForServlet", value = "/servlet/findgoodspageforservlet")
public class FindGoodsPageForServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(FindGoodsPageForServlet.class);
	private GoodsService goodsService = new GoodsServiceImp();

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindGoodsPageForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.write(
				"<!DOCTYPE html>      <html lang='en'>      <head>      	<meta charset='UTF-8'>      	<title>显示商品</title>      </head>      <body>       <table width='60%' align='center'  cellpadding='0' cellpadding='0'>");

		//获取当前页，如果没有，则默认为1
		String currentPageString = request.getParameter("currentPage");
		int currentPage=currentPageString==null?1:Integer.parseInt(currentPageString);
		
		//读取配置文件里的信息，设置关于商品的分页单位
		ServletContext servletContext = this.getServletContext();
		String pageUtilForGoodsString = servletContext.getInitParameter("PageUtilForGoods");
		int pageUtil = Integer.parseInt(pageUtilForGoodsString);

		PageUtilForGoods pageUtilForGoods = goodsService.findGoods(currentPage, pageUtil);
		List<Goods> goodsList = pageUtilForGoods.getGoodsList();

		if (goodsList.size() < 6) {
			for (int i = goodsList.size(); i < 6; i++) {
				Goods goods = new Goods("没有该商品");
				goodsList.add(goods);
			}
		}

		// 显示第一行 图片行
		out.write("<tr align='center'>");
		for (int i = 0; i < 3; i++) {
			if (goodsList.get(i).getGoodsDesc().equals("没有该商品")) {
				out.write("<td>&nbsp;</td>");
			} else {
				out.write("<td><image src='/project06" + goodsList.get(i).getGoodsImage() + "' width='30%'></td>");
			}

		}

		out.write("</tr>");
		// 显示第二行 图片的描述
		out.write("<tr align='center'>");
		for (int i = 0; i < 3; i++) {
			if (goodsList.get(i).getGoodsDesc().equals("没有该商品")) {
				out.write("<td>&nbsp;</td>");
			} else {
				out.write("<td><a href='"+request.getContextPath()+"/servlet/addshoppingcartforservlet?goods_id="+goodsList.get(i).getId()+"&goods_name="+goodsList.get(i).getGoodsName()+"'>添加到购物车</a>   <a href='/project06/servlet/findgoodsbyidforservlet?id="
						+ goodsList.get(i).getId() + "'     target='_blank'>查看</a></td>");
			}
		}
		out.write("</tr>");

		// 显示第二行 图片行
		out.write("<tr align='center'>");
		for (int i = 3; i < 6; i++) {
			if (goodsList.get(i).getGoodsDesc().equals("没有该商品")) {
				out.write("<td>&nbsp;</td>");
			} else {
				out.write("<td><image src='/project06" + goodsList.get(i).getGoodsImage() + "' width='30%'></td>");
			}
		}

		out.write("</tr>");
		// 显示第二行 图片的描述
		out.write("<tr align='center'>");
		for (int i = 3; i < 6; i++) {
			if (goodsList.get(i).getGoodsDesc().equals("没有该商品")) {
				out.write("<td>&nbsp;</td>");
			} else {
				out.write("<td><a href='"+request.getContextPath()+"/servlet/addshoppingcartforservlet?goods_id="+goodsList.get(i).getId()+"&goods_name="+goodsList.get(i).getGoodsName()+"'>添加到购物车</a>   <a href='/project06/servlet/findgoodsbyidforservlet?id="
						+ goodsList.get(i).getId() + "'   target='_blank'>查看</a></td>");
			}
		}
		out.write("</tr>");
		
		
		//显示分页信息
		out.write("<tr align='right'><td colspan='3'>共"+pageUtilForGoods.getPageNum()+"页&nbsp;&nbsp;&nbsp; ");
		
		out.write("<a href='"+request.getContextPath()+"/servlet/findgoodspageforservlet?currentPage="+pageUtilForGoods.getPrePage()+"'>上一页</a>  &nbsp;&nbsp;&nbsp;");
		out.write("<a href='"+request.getContextPath()+"/servlet/findgoodspageforservlet?currentPage=1'>第一页</a>  &nbsp;&nbsp;&nbsp;");
		
		for(int i=1;i<=pageUtilForGoods.getPageNum();i++) {
			out.write("<a href='"+request.getContextPath()+"/servlet/findgoodspageforservlet?currentPage="+i+"'>"+i+"</a>  &nbsp;&nbsp;&nbsp;");
		}
		
		out.write("<a href='"+request.getContextPath()+"/servlet/findgoodspageforservlet?currentPage="+pageUtilForGoods.getPageNum()+"'>最后一页</a>  &nbsp;&nbsp;&nbsp;");
		out.write("<a href='"+request.getContextPath()+"/servlet/findgoodspageforservlet?currentPage="+pageUtilForGoods.getNextpage()+"'>下一页</a>  &nbsp;&nbsp;&nbsp;");
		out.write("</tr>");
		
		
	
		out.write("</table>	");
		
		//显示我的足迹
		out.write("<a href='"+request.getContextPath()+"/servlet/showzujiforservlet' target='_blank'>我的足迹</a><br>");
		
		out.write("<a href='"+request.getContextPath()+"/servlet/watchshoppingcartforservlet' target='_blank'>查看购物车</a><br>");
		
		out.write("</body> </html>");
		
		logger.debug(" FindGoodsPageForServlet >doGet end.......");
	}

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindGoodsPageForServlet >doPost start.......");
		this.doGet(request, response);

		logger.debug(" FindGoodsPageForServlet >doPost end.......");
	}

}